package com.yy.permissions_general.module.system.role.controller;

import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.role.entity.Role;
import com.yy.permissions_general.module.system.role.service.RoleService;
import com.yy.permissions_general.utils.ResultUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 角色前端控制器
 * </p>
 *
 * @author YY
 * @since 2021-09-30
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
    * 查询列表
    * @param role
    * @param limit
    * @param page
    * @return
    */
    @RequestMapping("/list")
    @MyLog("查询角色")
    public ResultUtil list(Role role,@RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(roleService.selectPageList(role,limit,page));
    }

    /**
    * 新增数据
    * @param role
    * @return
    */
    @RequestMapping("/addRole")
    @MyLog("添加角色")
    @PreAuthorize("hasPermission('/role/addRole','role:addRole')")
    public ResultUtil addRole(Role role) {
        roleService.addRole(role);
        return ResultUtil.success();
    }

    /**
    * 修改数据
    * @param role
    * @return
    */
    @RequestMapping("/editRole")
    @MyLog("修改角色")
    @PreAuthorize("hasPermission('/role/editRole','role:editRole')")
    public ResultUtil editRole(Role role) {
        roleService.editRole(role);
        return ResultUtil.success();
    }

    /**
     * 修改状态
     * @param role
     * @return
     */
    @RequestMapping("/updateRole")
    @MyLog("修改角色状态")
    @PreAuthorize("hasPermission('/role/updateRole','role:updateRole')")
    public ResultUtil updateRole(Role role) {
        roleService.updateRole(role);
        return ResultUtil.success();
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @RequestMapping("/detailRole")
    @MyLog("查询角色信息")
    public ResultUtil detailRole(Integer id) {
        Role role = roleService.getDetailsById(id);
        return ResultUtil.success(role);
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    @MyLog("删除角色")
    @PreAuthorize("hasPermission('/role/delete','role:delete')")
    public ResultUtil delete(Integer id) {
        roleService.deleteById(id);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @RequestMapping("/deleteBatch")
    @MyLog("批量删除角色")
    @PreAuthorize("hasPermission('/role/deleteBatch','role:deleteBatch')")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        roleService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
